<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<script src="../../base/javascript/kissy-min.js"></script>
</head>
<body>


<button id='send'>发送</button>
<input id="msg"/>


<div id="ret" style="border:1px solid red;margin:10px;">
</div>


<script>
    var host = window.location.hostname;
    var base = window.location.href.replace(/[^/]*$/, "").replace(/https?:\/\//, "");
    var webSocket = new WebSocket('ws://' + host + ':8081/');
    var ret = KISSY.one("#ret"),
            ok = false,
            msg = KISSY.one("#msg"),
            send = KISSY.one("#send");
    webSocket.onopen = function(event) {
        ok = true;
        console.log("connected");
    };

    webSocket.onmessage = function(event) {
        ret.append("<div>" + event.data + "</div>");
    };

    send.on("click", function() {
        if (ok) {
            webSocket.send(msg.val());
        } else {
            alert("wait 1s to retry");
        }
    });
</script>
</body>
</html>